package es.deusto.ingenieria.is.search.touringproblem.formulation;


import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import es.deusto.ingenieria.is.search.formulation.State;
import es.deusto.ingenieria.is.search.xml.StateXMLReader;



	public class MapReader extends StateXMLReader {

		private int cityLocationX;
		private int cityLocationY;
		private String cityName;
		private int cities;
		private String type;
		private static List<City> distancias= new ArrayList<City>();
		
		
		public MapReader(String xmlFile) {
			super(xmlFile);
		}
		
		public State getState() {
			return new EnvironmentMap(this.distancias);
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			
			try {		
				if (qName.equals("is:map")) {
					this.cities = Integer.parseInt(attributes.getValue("cities"));
				} else if (qName.equals("is:distribution")){
				} else if ((qName.equals("is:start") || (qName.equals("is:end")))){
					this.cityName = attributes.getValue("name").toUpperCase();
					this.cityLocationX= (int)Integer.parseInt(attributes.getValue("x"));;
					this.cityLocationY= (int)Integer.parseInt(attributes.getValue("y"));
					if(cityName.equals("S")){
						this.type="start";
					}else{
						this.type="end";
					}
					City ciudad= new City(cityLocationX, cityLocationY, cityName, type);
					this.distancias.add(ciudad);
				} else if (qName.equals("is:city")){
					this.cityName = attributes.getValue("name").toUpperCase();
					this.cityLocationX= (int)Integer.parseInt(attributes.getValue("x"));;
					this.cityLocationY= (int)Integer.parseInt(attributes.getValue("y"));
					this.type="normal";
					City ciudad= new City(cityLocationX, cityLocationY, cityName, type);
					this.distancias.add(ciudad);			
				}
			} catch (Exception ex) {
				System.out.println(this.getClass().getName() + ".startElement(): " + ex);
			}
		}
	}